package com.coinworld.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ZhangRuiFeng on 2019/5/15.
 */

@EnableRabbit
@Configuration
public class RabbitMqConfig {

    public static final String EXCHANGE = "spring.boot.direct";
    public static final String ROUTINGKEY_FAIL = "spring.boot.routingKey.failure";
    public static final String ROUTINGKEY = "spring.boot.routingKey";
    public static final String QUEUE_NAME = "spring.demo";
    public static final String QUEUE_NAME_FAIL = "spring.demo.failure";

    //RabbitMQ的配置信息
    @Value("${spring.rabbitmq.host}")
    private String host;
    @Value("${spring.rabbitmq.port}")
    private Integer port;
    @Value("${spring.rabbitmq.username}")
    private String username;
    @Value("${spring.rabbitmq.password}")
    private String password;
    @Value("${spring.rabbitmq.virtual-host}")
    private String virtualHost;

    private static final String DEQUE_QUEUE_NAME = "dequeQueue";
    private static final String ENQUE_QUEUE_NAME = "enqueQueue";
    private static final String DEQUE_QUEUE_NAME_KEY = "dequeQueueKey";
    private static final String DELAY_EXCHANGE="exchange_delay";

    @Bean
    public Queue msgQueue(){
        return  new Queue("rewardQueue",true);
    }

    @Bean
    public Queue diligenceUpdate(){
        return  new Queue("diligenceUpdate",true);
    }

    //死信路由
    @Bean
    DirectExchange exchange() {
        return new DirectExchange(DELAY_EXCHANGE);
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory factory = new CachingConnectionFactory();
        factory.setUsername(username);
        factory.setPassword(password);
        factory.setVirtualHost(virtualHost);
        factory.setHost(host);
        factory.setPort(port);
        return factory;
    }


    //用于延时消费的队列
    @Bean
    public Queue dequeQueue() {
        Queue queue = new Queue(DEQUE_QUEUE_NAME,true,false,false);
        return queue;
    }

    //绑定exchange 到出队队列
    @Bean
    public Binding  deadLetterBinding() {
        return BindingBuilder.bind(dequeQueue()).to(exchange()).with(DEQUE_QUEUE_NAME_KEY);
    }

    //配置死信队列，即入队队列
    @Bean
    public Queue deadLetterQueue() {
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("x-message-ttl", 1000*60*60*24*365);
//        args.put("x-message-ttl", 1000*20);
        args.put("x-dead-letter-exchange", DELAY_EXCHANGE);
        args.put("x-dead-letter-routing-key", DEQUE_QUEUE_NAME_KEY);
        return new Queue(ENQUE_QUEUE_NAME, true, false, false, args);
    }

}
